
if (files.cwd().endsWith("脚本")) {
  let _require = require;
  require = function (path) {
    if (path.startsWith("./")) {
      path = path.replace("./", "");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!")
    while (true) {
      var allow = text("允许").findOne(5000)
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!")
        allow.click()
        sleep(1000)
        thread.interrupt()
        break
      }
    }
  })
  sleep(1000)
  var result = launchApp(app_name)
  sleep(3000)
  thread.interrupt()
  return result
}

// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}

function task() {
  var 发现 = text('发现').findOne(20000)
  if (发现) {
    sleep(time)
    var 我的 = text('我的').findOne(5000)
    if (我的) {
      我的.parent().click()
      sleep(2000)
      utils.swip_up()
      sleep(2000)
      var 观看广告 = text('观看广告').findOne(5000)
      if (观看广告) {
        var arr = 观看广告.parent().find(text('去完成'))
        if (arr.length > 0) {
          arr[0].parent().click()
          sleep(5000)
          var index = 0
          while (index < 5) {
            var 精彩瞬间 = text('精彩瞬间').findOne(2000)
            console.log('index = '+index)
            if (精彩瞬间) {
              var finalActivity = currentActivity();
              sleep(random(1000, 2000))
              click(random(380, 500), random(670, 700))
              console.log('进入广告')
              adPlaying();
              console.log('退出广告')
              sleep(random(1000, 2000))
              var nums = textEndsWith('/5').findOne(3000)
              if (nums) {
                var num = parseFloat(nums.text().slice(0, 1))
                console.log('num = '+num)
                if (num >= 5) {
                  break
                }
              }
              sleep(time)
              utils.swip_up();
              sleep(3000)
            }
            index++
            utils.updateAppProgress(index*20 - 10);
          }
        } else {
          toastLog('已经完成任务')
          sleep(time)
        }
      }
    }
  }
}

/**
 * 自己的广告特殊处理
 */
function adPlaying(){
  while(true){
    var ad_close = className("android.view.ViewGroup").clickable(true).depth(15).drawingOrder(1).indexInParent(0).findOne(2000);
    if(ad_close){
      点击控件(ad_close);
      return
    }
  }

}

  // 点击控件坐标，控件的clickbale为false的时候
  function 点击控件(x) {
    var weight = x.bounds()
    click(weight.centerX(), weight.centerY())
  }

function main() {
  let open_app = launchAppEnhance("PickTa");
  if (open_app) {
    task()
  }
  else {
    toastLog("PickTa打开失败，请确认是否安装并授权打开!");  
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");
